<template>
	<view class="mainpadding">
		<view class="ffffff radius mainpadding" v-for="item in list" :key="item.id">
			<view class="flexleft" v-if="item.is_sign==1">
				<view class="yqs">已签署</view>
			</view>
			<view class="margin_top">
				<view class="titletext hei_text yihang">{{item.name}}</view>
				<view class="margin_top xiaohui lianghang" v-if="item.description">{{item.description }}</view>
			</view>
			<view class="flexright margin_top">
				<view class="wqs" @click="qianshu(item)" v-if="item.is_sign==0">未签署，点击签署</view>
				<view class="wqs margin_left2" @click="downloadfile(item)" v-if="item.is_down==1">下载合同</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				list: []
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			downloadfile(item){
				let that = this
				uni.downloadFile({
					url: item.treatyinfo.contract_url_text, //下载地址接口返回
					success: (data) => {
						if (data.statusCode === 200) {
							//隐藏加载框
							uni.hideLoading();
							that.updatedownnum(item.id)
							//文件保存到本地
							uni.saveFile({
								tempFilePath: data.tempFilePath, //临时路径
								success: function(res) {
									uni.showToast({
										icon: 'success',
										mask: true,
										// title: '文件已保存：' + res.savedFilePath, //保存路径
										title: '下载成功',
										duration: 2000,
									});
									//自动打开文档查看
									setTimeout(() => {
										var filePath = res.savedFilePath;
										uni.openDocument({ //新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
											filePath: filePath,
											showMenu: true,
											success: function(res) {}
										});
									}, 2000)
								}
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '文件下载失败',
						});
					},
				});
			},
			updatedownnum(id){
				httpRequest.request('/api/user/updateDownNum', 'GET', {id}, false, false, true).then(res => {
				})
			},
			init() {
				httpRequest.request('/api/user/htList', 'GET', {}, false, false, true).then(res => {
					this.list = res.data
				})
			},
			qianshu(item){
				uni.navigateTo({
					url:"/pages/tab4/hetong/contractCopy2?id="+item.id
				})
				return false
				let userinfo = uni.getStorageSync("userInfo");
				let url = item.weburl +"&user_id=" +  userinfo.id;
				let data = {
					url
				}
				console.log(url,">>>>");
				uni.navigateTo({
					url:"/pages/webview/webview?msg="+encodeURIComponent(JSON.stringify(data))
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.wqs {
		border-radius: 50rpx;
		padding: 7px 15px;
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #c1282e;
		border: 0.5px solid #c1282e;
	}

	.yqs {
		background-color: #e8f6e6;
		color: #169E00;
		border-radius: 5px 5px 5px 0;
		padding: 2px 5px;
	}
</style>